/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.firstlife.mps.unittests;

import j2meunit.framework.Test;
import j2meunit.framework.TestCase;
import j2meunit.framework.TestMethod;
import j2meunit.framework.TestSuite;

import java.util.Vector;

import javax.microedition.lcdui.Alert;


import com.aroundj2me.model.Constants;
import com.aroundj2me.model.Place;
import com.aroundj2me.xml.XMLDecoder;

public class XMLDecoderTest extends TestCase {
	private XMLDecoder decoder;
	
	public XMLDecoderTest() {
	}
	
	public XMLDecoderTest(String string, TestMethod method) {
		super(string, method);
	}
	
	protected void setUp() throws Exception {
		decoder = new XMLDecoder();
	}

	protected void tearDown() throws Exception {
		decoder = null;
	}

	/**
	 * Checks specifically the retrieving of place-objects
	 * out of the xml-response string returned from the server
	 *
	 */
	public void testGetPlaces(){
		Place p1 = new Place();
		p1.setId(13);
		p1.setName("Pizzeria Torcia");
		p1.setAddress("Centro ...");
		p1.setCity("Bolzano");
		p1.setPhone("0471 934213");
		p1.setUser("kito");
		
		Place p2 = new Place();
		p2.setId(12);
		p2.setName("Pizzeria Chicken");
		p2.setAddress("Via Resia ...");
		p2.setCity("Bolzano");
		p2.setPhone("0471 932198");
		p2.setUser("kito2");
		
		StringBuffer dummyResponse = new StringBuffer();
		dummyResponse.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
		dummyResponse.append("<root>");
		dummyResponse.append("<type>" + Constants.DATA_PLACE + "</type>");
		dummyResponse.append("<responseBody>");
		dummyResponse.append("<place>");
		dummyResponse.append("<id>" + p1.getId() + "</id>");
		dummyResponse.append("<name>" + p1.getName() + "</name>");
		dummyResponse.append("<address>" + p1.getTitle() + "</address>");
		dummyResponse.append("<city>" + p1.getCity() + "</city>");
		dummyResponse.append("<phone>" + p1.getPhone() + "</phone>");
		dummyResponse.append("<user>" + p1.getUser() + "</user>");
		dummyResponse.append("</place>");
		dummyResponse.append("<place>");
		dummyResponse.append("<id>" + p2.getId() + "</id>");
		dummyResponse.append("<name>" + p2.getName() + "</name>");
		dummyResponse.append("<address>" + p2.getTitle() + "</address>");
		dummyResponse.append("<city>" + p2.getCity() + "</city>");
		dummyResponse.append("<phone>" + p2.getPhone() + "</phone>");
		dummyResponse.append("<user>" + p2.getUser() + "</user>");
		dummyResponse.append("</place>");
		dummyResponse.append("</responseBody>");
		dummyResponse.append("</root>");
		
		decoder.setXmlResponseString(dummyResponse.toString());
		try {
			decoder.parseXML();
		} catch (Exception e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
		
		Vector places = decoder.getPlaces();
		assertNotNull(places);
		
		assertEquals("Size of returned vector", 2, places.size());		
		Place p1Retrieved = (Place) places.elementAt(0);
		Place p2Retrieved = (Place) places.elementAt(1);
		
		assertEquals("Place p1 retrieved", p1.getTitle(), p1Retrieved.getTitle());
		assertEquals("Place p2 retrieved", p2.getTitle(), p2Retrieved.getTitle());
		
	}
	
	/**
	 * Checks if an Alert object is parsed out successfully
	 * out of the server response
	 *
	 */
	public void testGetAlert(){
		String alertMessage = "hello, message test";
		
		StringBuffer dummyResponse = new StringBuffer();
		dummyResponse.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
		dummyResponse.append("<root>");
		dummyResponse.append("<type>" + Constants.ALERT + "</type>");
		dummyResponse.append("<responseBody>");
		dummyResponse.append("<level>" + Constants.ALERT_ERROR + "</level>");
		dummyResponse.append("<message>" + alertMessage + "</message>");
		dummyResponse.append("</responseBody>");
		dummyResponse.append("</root>");
		
		decoder.setXmlResponseString(dummyResponse.toString());
		try {
			decoder.parseXML();
		} catch (Exception e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
		
		Alert parsedAlert = decoder.getAlert();
		
		assertNotNull(parsedAlert);
		assertEquals("Type should be " + Constants.ALERT, Constants.ALERT, decoder.getType());
		assertEquals("Alert-level should be " + Constants.ALERT_ERROR, Constants.ALERT_ERROR, parsedAlert.getTitle());
		assertEquals("Message should be equal", alertMessage, parsedAlert.getString());
		
	}


	public Test suite() {
		TestSuite suite = new TestSuite();
		suite.addTest(new XMLDecoderTest("XMLDecoderTest:testGetPlaces", new TestMethod() {
			public void run(TestCase tc) {
				((XMLDecoderTest) tc).testGetPlaces();
			}
		}));
		suite.addTest(new XMLDecoderTest("XMLDecoderTest:testGetAlert", new TestMethod() {
			public void run(TestCase tc) {
				((XMLDecoderTest) tc).testGetAlert();
			}
		}));
		return suite;
	}
}
